//
//  XMHomeDetailViewController.m
//  XMJJ_zml
//
//  Created by 杨何华 on 2025/5/12.
//

#import "XMCommentViewController.h"
#import "ProductBannerView.h"
#import "SDCycleScrollView.h"
#import "Masonry/Masonry.h"
#import "DescriptionSectionCell.h"
#import "ReviewSectionCell.h"
#import "ReviewCustomCell.h"
#import "PersonDetailViewCell.h"
#import "RatingInfoView.h"
#import "XMCommentViewController.h"
// 定义分区类型
typedef NS_ENUM(NSInteger, ProductDetailSectionType) {
    ProductDetailSectionTypeReviews,       // 评价区
    ProductDetailSectionTypeCount         // 分区总数
};


@interface XMCommentViewController ()<UITableViewDelegate,UITableViewDataSource,RatingInfoViewDelegate,SDCycleScrollViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) RatingInfoView *ratingView;
@end

@implementation XMCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 准备数据
    [self setupTableView];

    _ratingView = [[RatingInfoView alloc] initWithFrame:CGRectMake(8, 0, kScreenWidth-16, KScaleWidth(100))];
//      _ratingView.goodRate = 0.98;
//      _ratingView.allCount = 360000;
//      _ratingView.goodCount = 300000;
//      _ratingView.mediumCount = 30000;
//      _ratingView.badCount = 303;
//      _ratingView.picCount = 303;
      _ratingView.backgroundColor = [UIColor redColor];
      
    _ratingView.overallRating = 4.8;
    _ratingView.overallTitle = @"总体评分";
    _ratingView.delegate = self;
    _ratingView.cornerRadius = 10.0;
    _ratingView.itemSpacing = 8.0;
       
//      [self.view addSubview:_ratingView];
    [self.tableView setTableHeaderView:_ratingView];
    NSDictionary *sampleData = @{
         @"totalReviews": @"36万+",
         @"reviewTypes": @[
             @{@"type": @"全部评价", @"count": @"36万+"},
             @{@"type": @"好评", @"count": @"30万+"},
             @{@"type": @"中评", @"count": @"5万+"},
             @{@"type": @"差评", @"count": @"1万+"},
             @{@"type": @"有图", @"count": @"12万+"},
             @{@"type": @"追评", @"count": @"8万+"},
             @{@"type": @"视频", @"count": @"3万+"},
             @{@"type": @"晒单", @"count": @"5万+"},
             @{@"type": @"好评率", @"count": @"99%"}
         ]
     };
     
//
//      // 设置约束
//      [_ratingView mas_makeConstraints:^(MASConstraintMaker *make) {
//          make.top.equalTo(self.tableView.mas_safeAreaLayoutGuideTop);
//          make.left.equalTo(self.tableView);
//          make.right.equalTo(self.tableView);
//          make.height.mas_equalTo(200);
//      }];
      
//    [_ratingView updateWithData:sampleData];

      // 更新视图数据
//      [_ratingView updateViewData];
      
      [self getRecommendFunciton];
}
-(void)getRecommendFunciton
{
    
}
-(void)bannerTapped:(UITapGestureRecognizer *)tap {
    
}
- (void)setupTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreentHeight) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
//    _tableView.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:_tableView];
      _tableView.sectionHeaderHeight = 4;
     _tableView.sectionFooterHeight = 4;
    // 注册单元格
     [_tableView registerClass:[PersonDetailViewCell class] forCellReuseIdentifier:@"PersonDetailViewCell"];
 }

#pragma mark - UITableViewDelegate & UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     PersonDetailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonDetailViewCell" forIndexPath:indexPath];
     [cell setupWithAvatarUrl:@"https://picsum.photos/600/300" userName:@"1111" rating:1 reviewText:@"1123" dateString:@"123123" photoUrls:[NSArray arrayWithObjects:@"https://picsum.photos/600/300",@"https://picsum.photos/600/300",@"https://picsum.photos/600/300", nil] placeholderImage:[UIImage imageNamed:@"location"]];
//    cell.delegate = self;
    return cell;
}
-(void)goCommentAction:(ReviewCustomCell *)obj
{
    XMCommentViewController * detailVc = [XMCommentViewController new];
    [self.navigationController pushViewController:detailVc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
          case ProductDetailSectionTypeReviews:
            return [ReviewCustomCell calculateHeightForReviewText:@"1123" photoCount:3 width:100]+30;
        default:
            return 0;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"点击了第 %ld 张轮播图", (long)index);
    // 处理轮播图点击事件
}
@end
